package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.StartService;
import cn.web.tunnelrobot.dao.*;
import cn.web.tunnelrobot.dto.*;
import cn.web.tunnelrobot.pojo.*;
import cn.web.tunnelrobot.service.HomePageService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.EntityUtils;
import cn.web.tunnelrobot.util.MileageConversion;
import cn.web.tunnelrobot.util.PlanProgress;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import static cn.web.tunnelrobot.util.Constants.Meter;


@Service
public class HomePageServiceImpl implements HomePageService {
    @Autowired
    private ImageInfoDao imageInfoDao;
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private WarningRecordDao warningRecordDao;
    @Autowired
    private HardwareTestInfoDao hardwareTestInfoDao;
    //log4j
    Logger logger = LoggerFactory.getLogger(HomePageServiceImpl.class);
    DecimalFormat decimalFormat = new DecimalFormat("0.00");

    @Override
    public HashMap<String, Object> getTunnel(Integer pageNo, Integer prId) {
        int imConstructionStatus4 = 0;//1开挖、2初支、3二衬 、4已完成
        int imConstructionStatus3 = 0;
        int imConstructionStatus2 = 0;
        int imConstructionStatus1 = 0;
        int imConstructionStatus5 = 0;//未开挖  1仰拱、2已完成
        int imConstructionStatus6 = 0;//仰拱
        int imConstructionStatus7 = 0;//已完成
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count6 = 0;
        int count7 = 0;
        if (pageNo == null){
            pageNo = 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        Project project = projectDao.getProjectById(prId);
        if (project == null){
            map.put("prTunnelLength", 0);
            map.put("prTunnelLength", 0);
            map.put("imConstructionStatus1", 0);
            map.put("imConstructionStatus2", 0);
            map.put("imConstructionStatus3", 0);
            map.put("imConstructionStatus4", 0);
            map.put("imConstructionStatus5", 0);
            map.put("imConstructionStatus6", 0);
            map.put("imConstructionStatus7", 0);
            map.put("position", "DK0+0");

            return map;
        }
        Float prTunnelLength = project.getPrTunnelLength();
        int ceil = (int)Math.ceil(prTunnelLength / 1000);
        if (pageNo > ceil){
            map.put("code",-1);
            map.put("msg","共"+ceil+"页");
            return map;
        }
        map.put("prTunnelLength", prTunnelLength);
        ImageInfo imageInfo4 = imageInfoDao.getTunnel(1, 4, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);
        ImageInfo imageInfo3 = imageInfoDao.getTunnel(1, 3, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);
        ImageInfo imageInfo2 = imageInfoDao.getTunnel(1, 2, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);
        ImageInfo imageInfo1 = imageInfoDao.getTunnel(1, 1, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);
        ImageInfo imageInfo7 = imageInfoDao.getTunnel(2, 2, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);
        ImageInfo imageInfo6 = imageInfoDao.getTunnel(2, 1, prId,1000 * (pageNo - 1) + 1, 1000 * pageNo);

        if (imageInfo4 != null) {
            imConstructionStatus4 = imageInfo4.getImMileage();
        }
        if (imageInfo3 != null) {
            imConstructionStatus3 = imageInfo3.getImMileage();
        }
        if (imageInfo2 != null) {
            imConstructionStatus2 = imageInfo2.getImMileage();
        }
        if (imageInfo1 != null) {
            imConstructionStatus1 = imageInfo1.getImMileage();
        }
        if (imageInfo7 != null) {
            imConstructionStatus7 = imageInfo7.getImMileage();
        }
        if (imageInfo6 != null) {
            imConstructionStatus6 = imageInfo6.getImMileage();
        }

        //二衬
        HashMap<String, Object> twoLiningMap = new HashMap<>();
        //一衬
        HashMap<String, Object> oneLiningMap = new HashMap<>();
        //盾构机
        HashMap<String, Object> tbmMap = new HashMap<>();
        //挖掘机
        HashMap<String, Object> excavateMap = new HashMap<>();
        //卡车
        HashMap<String, Object> truckMap = new HashMap<>();
        //人物
        HashMap<String, Object> characterMap = new HashMap<>();

        HashMap<String, Object> modelMap = new HashMap<>();
        if (pageNo * 1000 < prTunnelLength) {
            int length1 = pageNo -1;
            int length2 = pageNo;
            map.put("position","DK"+length1+"+1~DK"+length2+"+0");
            count4 = imConstructionStatus4 / 10;
            map.put("imConstructionStatus4",count4);
            truckMap.put("imConstructionStatus4",count4);
            characterMap.put("imConstructionStatus4",count4);
            if (imConstructionStatus4 > imConstructionStatus3) {
                    map.put("imConstructionStatus3",0);
            } else {
                count3 = imConstructionStatus3 / 10;
                count3 = count3 - count4;
                map.put("imConstructionStatus3",count3);
                twoLiningMap.put("imConstructionStatus3",count3);
                modelMap.put("twoLining",twoLiningMap);
                characterMap.put("imConstructionStatus3",count3);
            }
            if (imConstructionStatus4 > imConstructionStatus2 || imConstructionStatus3 > imConstructionStatus2) {
                    map.put("imConstructionStatus2",0);
            } else {
                count2 = imConstructionStatus2 / 10;
                count2 = count2 - count4 - count3;
                map.put("imConstructionStatus2",count2);
                oneLiningMap.put("imConstructionStatus2",count2);
                modelMap.put("oneLining",oneLiningMap);
                characterMap.put("imConstructionStatus2",count2);
            }
            if (imConstructionStatus4 > imConstructionStatus1 || imConstructionStatus3 > imConstructionStatus1 || imConstructionStatus2 > imConstructionStatus1) {
                    map.put("imConstructionStatus1",0);
            } else {
                count1 = imConstructionStatus1 / 10;
                count1 = count1 - count2 - count4 - count3;
                map.put("imConstructionStatus1",count1);
                tbmMap.put("imConstructionStatus1",count1);
                excavateMap.put("imConstructionStatus1",count1);
                modelMap.put("tbm",tbmMap);
                modelMap.put("excavate",excavateMap);

                truckMap.put("imConstructionStatus1",count1);
                characterMap.put("imConstructionStatus1",count1);
            }
            map.put("imConstructionStatus",100 - count1 - count2 - count4 - count3);

            count7 = imConstructionStatus7 / 10;
            map.put("imConstructionStatus7",count7);
            if (imConstructionStatus7 > imConstructionStatus6) {
                map.put("imConstructionStatus6",0);
            } else {
                count6 = imConstructionStatus6 / 10;
                count6 = count6 - count7;
                map.put("imConstructionStatus6",count6);
            }
            map.put("imConstructionStatus5",100 - count7 - count6);
            modelMap.put("truck",truckMap);
            modelMap.put("character",characterMap);

            map.put("model",modelMap);
        } else {
            int length1 = (pageNo -1);
            map.put("position","DK"+length1+"+1~DK"+length1+"+"+decimalFormat.format(prTunnelLength - (pageNo-1)*1000));
            float remainder = (prTunnelLength - (pageNo - 1) * 1000) % 10;
            int countTotal = (int)((prTunnelLength - (pageNo-1) * 1000) / 10);
            if (remainder > 0){
                countTotal++;
            }
            System.out.println("countTotal:" + countTotal);
            count4 = imConstructionStatus4 / 10;
            map.put("imConstructionStatus4", count4);
            if (imConstructionStatus4 > imConstructionStatus3) {
                map.put("imConstructionStatus3", 0);
            } else {
                count3 = imConstructionStatus3 / 10;
                count3 = count3 - count4;
                map.put("imConstructionStatus3", count3);
            }
            if (imConstructionStatus4 > imConstructionStatus2 || imConstructionStatus3 > imConstructionStatus2) {
                map.put("imConstructionStatus2", 0);
            } else {
                count2 = imConstructionStatus2 / 10;
                count2 = count2 - count4 - count3;
                map.put("imConstructionStatus2", count2);
            }
            if (imConstructionStatus4 > imConstructionStatus1 || imConstructionStatus3 > imConstructionStatus1 || imConstructionStatus2 > imConstructionStatus1) {
                map.put("imConstructionStatus1", 0);
            } else {
                count1 = imConstructionStatus1 / 10;
                count1 = count1 - count2 - count4 - count3;
                map.put("imConstructionStatus1", count1);
            }
            map.put("imConstructionStatus", countTotal - count1 - count2 - count4 - count3);
            count7 = imConstructionStatus7 / 10;
            map.put("imConstructionStatus7",count7);
            if (imConstructionStatus7 > imConstructionStatus6) {
                map.put("imConstructionStatus6",0);
            } else {
                count6 = imConstructionStatus6 / 10;
                count6 = count6 - count7;
                map.put("imConstructionStatus6",count6);
            }
            map.put("imConstructionStatus5",countTotal - count7 - count6);
        }
       /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        UserRecord userRecord = new UserRecord();
        if (user2 != null){
            userRecord.setUsId(user2.getUsId());
        }else {
            userRecord.setUsId(-1);
        }
        userRecord.setUrOperatContent("进入首页");
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
        return map;
    }

    @Override
    public HashMap<String, Object> getTunnelInformation(Integer prId) {

        //隧道下半部
        int imConstructionStatus7 = 0;//已完成（下半部分）
        int imConstructionStatus6 = 0;//仰拱（下半部分 ）
        int imConstructionStatus5 = 0;//未开挖（下半部分）

        //隧道上半部
        int imConstructionStatus4 = 0;//已完成（上半部分）
        int imConstructionStatus3 = 0;//二衬（上半部分）
        int imConstructionStatus2 = 0;//初支（上半部分）
        int imConstructionStatus1= 0;//开挖（上半部分）
        int imConstructionStatus= 0;//未开挖（上半部分）


        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //获取隧道信息
        Project project = projectDao.getProjectById(prId);
        if (project == null){
            map.put("prTunnelLength", 0);
            map.put("imConstructionStatus", 0);
            map.put("imConstructionStatus1", 0);
            map.put("imConstructionStatus2", 0);
            map.put("imConstructionStatus3", 0);
            map.put("imConstructionStatus4", 0);
            map.put("imConstructionStatus5", 0);
            map.put("imConstructionStatus6", 0);
            map.put("imConstructionStatus7", 0);
            return map;
        }
        //隧道名称
        String name=project.getPrName();
        //隧道长度
        Float prTunnelLength = project.getPrTunnelLength();
        //初始里程
        Float prInitialPosition=project.getInitial();
        //尾部里程
        Float prTailPosition=project.getTail();
        //隧道节数
        int total= (int) (prTunnelLength/Meter);
        float a=prTunnelLength%Meter;
        if(a!=0){
            total=total+1;
        }


        //二衬初始位置
        Float  prEcInitialPosition=project.getEcInitial();
        //初支初始位置
        Float prCzInitialPosition=project.getCzInitial();
        //开挖初始位置
        Float prKwInitialPosition=project.getKwInitial();
        //未开挖初始位置
        Float prWkwInitialPosition=project.getKwTail();

        //下部
        //仰拱初始位置
        Float prYgInitialPosition=project.getEcInitial();
//        //未开挖初始位置
//        Float prWkwInitialPosition=project.getKwTail();


        //上半部 从第零节隧道开始
        //已完成(上半部分)
        imConstructionStatus4=0;
        //二衬
        int aa=(int)((prEcInitialPosition-prInitialPosition)/Meter);
        float bb=(prEcInitialPosition-prInitialPosition)%Meter;
        imConstructionStatus3=aa;
        if(bb>(Meter/2)){
            imConstructionStatus3=imConstructionStatus3+1;
        }
        //初支(上半部分)
        aa=(int)((prCzInitialPosition-prInitialPosition)/Meter);
        bb=(prCzInitialPosition-prInitialPosition)%Meter;
        imConstructionStatus2=aa;
        if(bb>(Meter/2)){
            imConstructionStatus2=imConstructionStatus2+1;
        }
        //开挖(上半部分)
        aa=(int)((prKwInitialPosition-prInitialPosition)/Meter);
        bb=(prKwInitialPosition-prInitialPosition)%Meter;
        imConstructionStatus1=aa;
        if(bb>(Meter/2)){
            imConstructionStatus1=imConstructionStatus1+1;
        }
        //未开挖(上半部分)
        aa=(int)((prWkwInitialPosition-prInitialPosition)/Meter);
        bb=(prWkwInitialPosition-prInitialPosition)%Meter;
        imConstructionStatus=aa;
        if(bb>(Meter/2)){
            imConstructionStatus=imConstructionStatus+1;
        }

        //下半部 从第零节隧道开始
        //已完成（下半部分）
        imConstructionStatus7=0;
        //仰拱（下半部分 ）
        imConstructionStatus6=imConstructionStatus3;
        //未开挖（下半部分）
        imConstructionStatus5=imConstructionStatus;


//        map.put("prTunnelLength", prTunnelLength);
        //二衬支架
        HashMap<String, Object> twoLiningMap = new HashMap<>();
        //初支支架
        HashMap<String, Object> oneLiningMap = new HashMap<>();
        //盾构机
        HashMap<String, Object> tbmMap = new HashMap<>();
        //挖掘机
        HashMap<String, Object> excavateMap = new HashMap<>();
        //卡车
        HashMap<String, Object> truckMap = new HashMap<>();
        //人物
        HashMap<String, Object> characterMap = new HashMap<>();
        LinkedHashMap<String, Object> modelMap = new LinkedHashMap<>();

        //二衬支架
        twoLiningMap.put("location01",imConstructionStatus2-1);
        //初支支架
        oneLiningMap.put("location01",imConstructionStatus1-1);
        //盾构机
        tbmMap.put("location01",imConstructionStatus-1);
        //挖掘机
        excavateMap.put("location01",imConstructionStatus-1);
        //卡车
        truckMap.put("location01",imConstructionStatus3-1);
        truckMap.put("location02",imConstructionStatus-1);
        //人物
        characterMap.put("location01",imConstructionStatus3-1);
        characterMap.put("location02",imConstructionStatus2-1);
        characterMap.put("location03",imConstructionStatus1-1);
        characterMap.put("location04",imConstructionStatus-1);
        modelMap.put("twoLining",twoLiningMap);
        modelMap.put("oneLining",oneLiningMap);
        modelMap.put("tbm",tbmMap);
        modelMap.put("excavate",excavateMap);
        modelMap.put("truck",truckMap);
        modelMap.put("character",characterMap);

        map.put("name",name);
        map.put("TunnelLength",prTunnelLength);
        map.put("prInitialPosition",prInitialPosition);
        map.put("prTailPosition",prTailPosition);
        map.put("total",total);

        map.put("imConstructionStatus7",imConstructionStatus7);
        map.put("imConstructionStatus6",imConstructionStatus6);
        map.put("imConstructionStatus5",imConstructionStatus5);
        map.put("imConstructionStatus4",imConstructionStatus4);
        map.put("imConstructionStatus3",imConstructionStatus3);
        map.put("imConstructionStatus2",imConstructionStatus2);
        map.put("imConstructionStatus1",imConstructionStatus1);
        map.put("imConstructionStatus",imConstructionStatus);
        map.put("model",modelMap);
        return map;
    }

    /*@Override
    public HashMap<String, Object> getConstructionProgress(Integer prId) {
        HashMap<String, Object> map = new HashMap<>();
        Project projectById = projectDao.getProjectById(prId);
        if (prId == 0 || projectById == null){
            map.put("code",0);
            map.put("work", "DK0+0");
            map.put("Invert","DK0+0");
            map.put("second","DK0+0");
            map.put("workPercent",0);
            map.put("InvertPercent",0);
            map.put("secondPercent",0);
            return map;
        }
        ImageInfo imageInfo1 = imageInfoDao.getConstructionProgress(prId,1,1);//开挖
        ImageInfo imageInfo3 = imageInfoDao.getConstructionProgress(prId,1,3);//二衬
        ImageInfo imageInfo5 = imageInfoDao.getConstructionProgress(prId,2,1);//仰拱

        *//* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        UserRecord userRecord = new UserRecord();
        if (user2 != null){
            userRecord.setUsId(user2.getUsId());
        }else {
            userRecord.setUsId(-1);
        }
        userRecord.setUrOperatContent("进入首页");
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*//*

        float length = projectById.getPrTunnelLength();//隧道长度
        int imConstructionStatus13 = 0;
        int imConstructionStatus11 = 0;
        int imConstructionStatus21 = 0;

        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (imageInfo3 != null){
            imConstructionStatus13 = imageInfo3.getImMileage();//二衬
        }
        if (imageInfo1 != null){
            imConstructionStatus11 = imageInfo1.getImMileage();//开挖
        }
        if (imageInfo5 != null){
            imConstructionStatus21 = imageInfo1.getImMileage();//仰拱
        }
        //开挖，开挖状态 >= （已完成、二衬、初支）
        String format11 = decimalFormat.format(imConstructionStatus11 / length);
        map.put("workPercent",(int)(Float.parseFloat(format11) * 100));//开挖（上半部分）
        String format21 = decimalFormat.format(imConstructionStatus21 / length);
        map.put("InvertPercent",(int)(Float.parseFloat(format21) * 100));//仰拱（下半部分）
        map.put("second",MileageConversion.mileage(imConstructionStatus13));
        String format13 = decimalFormat.format(imConstructionStatus13 / length);
        map.put("secondPercent",(int)(Float.parseFloat(format13) * 100));//二衬（上半部分）
        map.put("code",0);
        map.put("work", MileageConversion.mileage(imConstructionStatus11));
        map.put("Invert",MileageConversion.mileage(imConstructionStatus21));
        return map;
    }*/

    /**
     * 获取项目进度
     * gbq
     * 二期
     */
    @Override
    public HashMap<String, Object> getProjectProgressById(Integer prId){
        HashMap<String,Object> map = new HashMap<String,Object>();
        ProjectDTO3 project = projectDao.getProjectProgressById(prId);
        if (project != null){
            map.put("code",0);
            map.put("data",project);
        }else {
            map.put("code",-1);
            map.put("msg","获取失败");
        }
        return map;
    }

    /**
     * 修改项目进度
     * gbq
     * 二期
     * */
    @Override
    public HashMap<String, Object> updateProjectProgress(Project1 project){
        Project project1 = projectDao.getProjectById(project.getPrId());
        /*开挖进度*/
        Float kwp=(float)((project.getD7()*1000+project.getD7_1())-(project.getD1())*1000+project.getD1_1())/project1.getPrTunnelLength();
        project1.setKwProgress(kwp);

        /*初支进度*/
        Float czp=(float)((project.getD5()*1000+project.getD5_1())-(project.getD1())*1000+project.getD1_1())/project1.getPrTunnelLength();
        project1.setCzProgress(czp);

        /*二衬进度*/
        Float ecp=(float)((project.getD3()*1000+project.getD3_1())-(project.getD1())*1000+project.getD1_1())/project1.getPrTunnelLength();
        project1.setEcProgress(ecp);

        /*开挖距离掌子面*/
        Float kwh=(float)0;
        project1.setKwHeadingFace(kwh);

        /*初支距离掌子面*/
        Float czh=(float)((project.getD8()*1000+project.getD8_1())-(project.getD6())*1000+project.getD6_1());
        project1.setCzHeadingFace(czh);

        /*二衬距离掌子面*/
        Float ech=(float)((project.getD8()*1000+project.getD8_1())-(project.getD4())*1000+project.getD4_1());
        project1.setEcHeadingFace(ech);


        PlanProgress p=new PlanProgress();
        String kwplan=p.planProgress(((project.getD7()*1000+project.getD7_1())-(project.getD13())*1000+project.getD13_1()));
        project1.setKwPlanProgress(kwplan);
        String czplan=p.planProgress(((project.getD5()*1000+project.getD5_1())-(project.getD11())*1000+project.getD11_1()));
        project1.setCzPlanProgress(czplan);
        String ecplan=p.planProgress(((project.getD3()*1000+project.getD3_1())-(project.getD9())*1000+project.getD9_1()));
        project1.setEcPlanProgress(ecplan);

        HashMap<String,Object> map = new HashMap<String,Object>();
     /*   if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }*/

        project1.setPrCreateDate(new Date());

        int result = projectDao.updateProjectProgress(project1);
        if (result == 1){
            map.put("msg","修改成功");
            map.put("code",0);
            StartService.projectDTO = projectDao.getDetailProjectById(1);//更新项目信息
          /*  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("修改项目信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }


    @Override
    public HashMap<String, Object> homePageShuffling() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int count = warningRecordDao.getWarningRecordListCount(null,null,null,null,null);
            if(count < 30){
                logger.info("警告记录小于30条!");
                logger.info("警告记录小于30条!");
                logger.info("警告记录小于30条!");
                List<WarningRecordDTO> warningRecordDTOList = new ArrayList<>();
        /*        String [] wrLevel = {"I","II","III","IV","V"};
                String [] wrDescribe = {"当前温度过高(模拟数据)","当前湿度过高(模拟数据)","当前氧气浓度过低(模拟数据)","当前光照强度过低(模拟数据)","当前粉尘浓度过高(模拟数据)","当前瓦斯浓度过高(模拟数据)","当前电源电量过低(模拟数据)"};
                String [] wrStatusName = {"未确认","已确认"};
                for (int i = 0 ;i < 30 ; i++){
                    WarningRecordDTO warningRecordDTO = new WarningRecordDTO();
                    int index = (int) (Math.random() * wrLevel.length);
                    int index2 = (int) (Math.random() * wrDescribe.length);
                    int index3 = (int) (Math.random() * wrStatusName.length);
                    String rand = wrLevel[index];
                    String rand2 = wrDescribe[index2];
                    String rand3 = wrStatusName[index3];
                    warningRecordDTO.setWrLevel(rand);
                    warningRecordDTO.setWrDescribe(rand2);
                    warningRecordDTO.setWrWarningTime(new Date());
                    warningRecordDTO.setWrAffirmStatusName(rand3);
                    warningRecordDTOList.add(warningRecordDTO);
                }
                List<List<WarningRecordDTO>> warningRecordDTOLists = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList1 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList2 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList3 = new ArrayList<>();
                for (int i =0 ;i<warningRecordDTOList.size();i++){
                    if(i<10){
                        warningRecordDTOList1.add(warningRecordDTOList.get(i));
                    }else if(i>9 && i< 20){
                        warningRecordDTOList2.add(warningRecordDTOList.get(i));
                    }else if(i>19 && i< 30){
                        warningRecordDTOList3.add(warningRecordDTOList.get(i));
                    }
                }*/
                List<List<WarningRecordDTO>> warningRecordDTOLists = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList1 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList2 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList3 = new ArrayList<>();
                warningRecordDTOLists.add(warningRecordDTOList1);
                warningRecordDTOLists.add(warningRecordDTOList2);
                warningRecordDTOLists.add(warningRecordDTOList3);
                map.put("code",0);
                map.put("data",warningRecordDTOLists);
            }else{
                //获得查询得数据
                List<WarningRecordDTO> warningRecordDTOList = warningRecordDao.homePageShuffling();
                List<List<WarningRecordDTO>> warningRecordDTOLists = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList1 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList2 = new ArrayList<>();
                List<WarningRecordDTO> warningRecordDTOList3 = new ArrayList<>();
                for (int i =0 ;i<warningRecordDTOList.size();i++){
                    if(i<10){
                        warningRecordDTOList1.add(warningRecordDTOList.get(i));
                    }else if(i>9 && i< 20){
                        warningRecordDTOList2.add(warningRecordDTOList.get(i));
                    }else if(i>19 && i< 30){
                        warningRecordDTOList3.add(warningRecordDTOList.get(i));
                    }
                }
                warningRecordDTOLists.add(warningRecordDTOList1);
                warningRecordDTOLists.add(warningRecordDTOList2);
                warningRecordDTOLists.add(warningRecordDTOList3);
                map.put("code",0);
                map.put("data",warningRecordDTOLists);
            }
        } catch (Exception e) {
            map.put("code",-1);
            map.put("data","查询警告信息时出错!");
            e.printStackTrace();
        }
        return map;
    }
    @Override
    public HashMap<String, Object> TunnelInspection(Integer prId, Integer htMileage, Float htDailySettl1, Float htDailySettl2, Float htDailyConvergence1, Float htDailyConvergence2, String time1, String time2, Integer pageNo, Integer pageSize,Integer export) {
        HashMap<String, Object> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        Date date2 = null;
        if (!StringUtils.isEmpty(time1)){
            try {
                date1 = simpleDateFormat.parse(time1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(time2)){
            try {
                date1 = simpleDateFormat.parse(time2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Integer htMileage1 = null;
        if (htMileage != null){
            map.put("htMileage",MileageConversion.mileage(htMileage));
            htMileage1 = htMileage - 10;
        }

        List<HardwareTestInfoDTO> list = new ArrayList<>();
        if(export == 1){
            list = hardwareTestInfoDao.getHardwareTestexport1(prId,htMileage1,htMileage,htDailySettl1,htDailySettl2,htDailyConvergence1,htDailyConvergence2,date1,date2);
        }else{
            list = hardwareTestInfoDao.getHardwareTestInfo1(prId,htMileage1,htMileage,htDailySettl1,htDailySettl2,htDailyConvergence1,htDailyConvergence2,date1,date2,(pageNo-1)*pageSize,pageSize);
        }

        map.put("htAxleWire",0);
        float cumulativeSettl = 0;//累计沉降值
        float cumulativeConvergence = 0;//累计收敛值
        int count = 0;
        if(list.size() > 0){
            //获取最后一条中轴线
            map.put("htAxleWire",list.get(0).getHtAxleWire());
            for (HardwareTestInfoDTO dto:list) {
                if (dto.getHtDailySettl() != null){
                    cumulativeSettl = cumulativeSettl + dto.getHtDailySettl();
                }
                if (dto.getHtDailyConvergence() != null){
                    cumulativeConvergence = cumulativeConvergence + dto.getHtDailyConvergence();
                }
            }
            count = hardwareTestInfoDao.getHardwareTestInfoCount1(prId,htMileage1,htMileage,htDailySettl1,htDailySettl2,htDailyConvergence1,htDailyConvergence2,date1,date2);
            map.put("data",list);
            map.put("count",count);
            map.put("code",0);
            map.put("code1",0);
            map.put("msg", "");
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            map.put("cumulativeSettl",decimalFormat.format(cumulativeSettl));
            map.put("cumulativeConvergence",decimalFormat.format(cumulativeConvergence));
        }else {
            map.put("htAxleWire",0);
            map.put("cumulativeSettl",0);
            map.put("cumulativeConvergence",0);
            map.put("code",0);
            map.put("code1",-1);
            map.put("msg", "暂无列表信息");
        }
        //cumulativeSettl   累计沉降值   cumulativeConvergence  累计收敛值
        //小数点转换工具==累计沉降\收敛

        return map;
    }

    @Override
    public ResponseEntity<byte[]> employee2Excel(List<HardwareTestInfoDTO> list){
        //1. 创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2. 创建文档摘要
        workbook.createInformationProperties();
        //5. 创建样式
        //创建标题行的样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFCellStyle dateCellStyle = workbook.createCellStyle();
        dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
        HSSFSheet sheet = workbook.createSheet("预警值沉降、收敛值记录");
        //设置列的宽度
//        sheet.setColumnWidth(0, 5 * 256);
//        sheet.setColumnWidth(1, 12 * 256);
//        sheet.setColumnWidth(2, 10 * 256);
//        sheet.setColumnWidth(3, 5 * 256);
//        sheet.setColumnWidth(4, 12 * 256);
//        sheet.setColumnWidth(5, 20 * 256);
        //6. 创建标题行
        HSSFRow r0 = sheet.createRow(0);
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("序号");
        c0.setCellStyle(headerStyle);
        HSSFCell c1 = r0.createCell(1);
        c1.setCellStyle(headerStyle);
        c1.setCellValue("时间");
        HSSFCell c2 = r0.createCell(2);
        c2.setCellStyle(headerStyle);
        c2.setCellValue("位置");
        HSSFCell c3 = r0.createCell(3);
        c3.setCellStyle(headerStyle);
        c3.setCellValue("当前沉降值");
        HSSFCell c4 = r0.createCell(4);
        c4.setCellStyle(headerStyle);
        c4.setCellValue("累计沉降值");
        HSSFCell c5 = r0.createCell(5);
        c5.setCellStyle(headerStyle);
        c5.setCellValue("当前收敛值");
        HSSFCell c6 = r0.createCell(6);
        c6.setCellStyle(headerStyle);
        c6.setCellValue("累计收敛值");

        //7.循环你的集合进行存储数据
        for (int i = 0; i < list.size(); i++) {
            HardwareTestInfoDTO hardware = list.get(i);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createData = sdf.format(hardware.getHtCreateDate());

            String htDailySettl;
            if(hardware.getHtDailySettl() == null){
                htDailySettl = "0.0";
            }else {
                htDailySettl = String.valueOf(hardware.getHtDailySettl());
            }

            String htCumulativeSettl;
            if(hardware.getHtCumulativeSettl() == null){
                htCumulativeSettl = "0.0";
            }else {
                htCumulativeSettl = String.valueOf(hardware.getHtCumulativeSettl());
            }

            String htDailyConvergence;
            if(hardware.getHtDailyConvergence() == null){
                htDailyConvergence = "0.0";
            }else {
                htDailyConvergence = String.valueOf(hardware.getHtDailyConvergence());
            }

            String htCumulativeConvergence;
            if(hardware.getHtCumulativeConvergence() == null){
                htCumulativeConvergence = "0.0";
            }else {
                htCumulativeConvergence = String.valueOf(hardware.getHtCumulativeConvergence());
            }

            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(hardware.getHtId());
            row.createCell(1).setCellValue(createData);
            row.createCell(2).setCellValue(hardware.getHtTurnMeter1());
            row.createCell(3).setCellValue(htDailySettl);
            row.createCell(4).setCellValue(htCumulativeSettl);
            row.createCell(5).setCellValue(htDailyConvergence);
            row.createCell(6).setCellValue(htCumulativeConvergence);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HttpHeaders headers = new HttpHeaders();
        try {
            headers.setContentDispositionFormData("attachment", new String("预警值沉降、收敛值记录.xls".getBytes("UTF-8"), "ISO-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            workbook.write(baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
    }

    @Override
    public HashMap<String, Object> SettlAndConvergence(Integer prId, Integer pageNo, Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        //近30天的沉降收敛值
        List<HardwareTestInfoDTO> list = hardwareTestInfoDao.getHardwareTestInfo(prId,null,null,null,null,null,null,null, (pageNo - 1) * pageSize, pageSize);
        if (list.size() > 0){
            int count = hardwareTestInfoDao.getHardwareTestInfoCount(prId,null,null,null,null,null,null,null);
            map.put("code",0);
            map.put("data",list);
            map.put("count",count);
            map.put("msg", "");
        }else{
            map.put("code",-1);
            map.put("msg", "暂无列表信息");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getProjectIntro() {
        HashMap<String, Object> map = new HashMap<>();
        Project project = projectDao.getProjectById(1);
        if (project != null){
            map.put("code",0);
            map.put("data",project.getPrIntro());
        }else {
            map.put("code",-1);
            map.put("msg","无简介");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getFive(Integer type){
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<HardwareTestInfoDTO> list = new ArrayList<>();
        if(type == 1){
            list = hardwareTestInfoDao.getNowFive();
        }else{
            list = hardwareTestInfoDao.getAllFive();
        }

        if(list.size() > 0){
            String[] number= new String[5];
            String[] time= new String[5];
            for (int i=0; i < list.size(); i++) {
                if(type == 1){
                    String htDailySettl;
                    if(list.get(i).getHtDailySettl() == null){
                        htDailySettl = "0.0";
                    }else {
                        htDailySettl = String.valueOf(list.get(i).getHtDailySettl());
                    }
                    number[i] = htDailySettl;
                }else{
                    String htCumulativeSettl;
                    if(list.get(i).getHtCumulativeSettl() == null){
                        htCumulativeSettl = "0.0";
                    }else {
                        htCumulativeSettl = String.valueOf(list.get(i).getHtCumulativeSettl());
                    }
                    number[i] = htCumulativeSettl;
                }

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String createData = sdf.format(list.get(i).getHtCreateDate());
                time[i] = createData;
            }
            map.put("code",0);
            map.put("number",number);
            map.put("time",time);
        }else {
            map.put("code",-1);
            map.put("msg","暂无信息");
        }
        return map;
    }

    public static void main(String[] args) {
        System.out.println( (8753 - (9 - 1) * 1000) % 10);
        System.out.println( (8753.8 - (9 - 1) * 1000) % 10);
        System.out.println( (8750 - (9 - 1) * 1000) % 10);

    }
}
